package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.OfertaDAO;
import br.urca.model.Oferta;

@Transactional
public class JPAOfertaDAO implements OfertaDAO {
	
	private EntityManager manager;
	
	public JPAOfertaDAO() {
		this(null);
	}
	
	@Inject
	public JPAOfertaDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Oferta> todos() {
		return this.manager.createQuery("SELECT o FROM Oferta o", Oferta.class)
				.getResultList();
	}

	@Override
	public Oferta buscarPorOfertaPedido(String codOferta, String codPedido) {
		try {
			return this.manager.createQuery("SELECT o FROM Oferta o "
					+ "WHERE o.id.codOferta = :oferta AND o.id.codPedido = :pedido", Oferta.class)
					.setParameter("oferta", codOferta)
					.setParameter("pedido", codPedido)
					.getSingleResult();
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Oferta> buscarPorOfertaDisciplina(String codOferta,
			String codDisciplina) {
		try {
			return this.manager.createQuery("SELECT o FROM Oferta o "
					+ "WHERE o.id.codOferta = :oferta AND o.codDisciplina = :disciplina", Oferta.class)
					.setParameter("oferta", codOferta)
					.setParameter("disciplina", codDisciplina)
					.getResultList();
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
